function initApp() {
	var data = {};
	Ext.Ajax.requestSyn({
				url : "queryAllBaseFunction.action",
				success : function(resp) {
					data = Ext.decode(resp);
				},
				error : function() {
					Ext.Msg.alert("错误", "系统异常,请联系管理员!")
				}
			})
	var rootNode = totlePower_treePanel.getRootNode();
	Ext.each(data.list, function(obj) {
				var node = new Ext.tree.TreeNode({
							text : obj.functionName,
							checked : false,
							leaf : true,
							id : obj.functionCode
						})
				rootNode.appendChild(node)
			})
}

var userTypeMap = new Ext.util.MixedCollection();
userTypeMap.add('E', '系统管理员');

var totlePower_treePanel = new Ext.tree.TreePanel({
			title : '所有功能',
			id : 'totlePower_treePanel',
			root : new Ext.tree.TreeNode({
						text : '所有权限',
						expanded : true,
						checked : false
					}),
			loader : new Ext.tree.TreeLoader(),
			rootVisible : false
		})

var userPower_treePanel =new  Ext.tree.TreePanel({
			title : '已配置功能',
			id : 'userPower_treePanel',
			root : new Ext.tree.TreeNode({
						text : '所有权限',
						expanded : true,
						checked : false
					}),
			loader : new Ext.tree.TreeLoader(),
			rootVisible : false
		})

var userCombo = new Ext.form.ComboBox({
	mode : 'local',
	triggerAction : 'all',
	editable : false,
	store : new Ext.data.JsonStore({
				url : 'queryUserByUserType.action',
				baseParams : {
					json : 'E'
				},
				root : 'list',
				autoLoad : true,
				fields : ['userId','userName', 'userType', 'registerDate', 'userId']
			}),
	valueField : 'userId',
	displayField : 'userName',
	fieldLabel : '用户名',
	listeners : {
		select : function(combo, record, index) {
			var store = combo.getStore();
			var username = record.get('userName');
			store.findBy(function(rd) {
				if (rd.get('userName') == username) {
					Ext.getCmp('userType_TL').setValue(userTypeMap.get(rd
							.get('userType')));
					Ext.getCmp('registerDate_DF').setValue(rd
							.get('registerDate'));

					// 清空用户权限树列表
					userPower_treePanel.getEl().mask('正在加载');
					var root=userPower_treePanel.getRootNode();
					root.removeAll();
					Ext.Ajax.requestSyn({
								url : "queryUserFunction.action",
								params : {
									userId : rd.get('userId')
								},
								success : function(resp) {
									var data = Ext.decode(resp);

									// 加载用户权限树列表
									Ext.each(data.list, function(obj) {
										var node = new Ext.tree.TreeNode({
												text : obj.functionName,
												checked : false,
												leaf : true,
												id : obj.functionCode
											})
										root.appendChild(node)
									})
									// 展开用户拥有权限
									root.expand();
								},
								error : function() {
									Ext.Msg.alert("错误", "系统异常,请联系管理员!")
								}
							})
					userPower_treePanel.getEl().unmask();

				}
			});
		}
	}
})
Ext.onReady(function() {
			initApp();

			Ext.QuickTips.init();

			var userInfo_panel = new Ext.Panel({
						frame : true,
						layout : 'column',
						defaults : {
							columnWidth : .32,
							xtype : 'form',
							labelWidth : 60,
							defaults : {
								anchor : '85%'
							}
						},
						items : [{
									items : [userCombo]
								}, {
									items : [{
												xtype : 'textfield',
												id : 'userType_TL',
												disabled : true,
												fieldLabel : '用户角色'
											}]
								}, {
									items : [{
												xtype : 'datefield',
												disabled : true,
												id : 'registerDate_DF',
												fieldLabel : '注册时间'
											}]
								}]
					});

			var powerPanel = new Ext.Panel({
						frame : true,
						height : 350,
						layout : 'column',
						title : '权限配置',
						defaults : {
							style : 'margin :10px',
							xtype : 'treepanel',
							columnWidth : .43,
							frame : true,
							border : true,
							height : 250
						},
						items : [totlePower_treePanel, {
							xtype : 'panel',
							style : '',
							columnWidth : .14,
							layout : 'vbox',
							layoutConfig : {
								align : 'center',
								pack : 'center'
							},
							border : false,
							frame : false,
							defaults : {
								xtype : 'button',
								anchor : '60%'
							},
							items : [{
								text : '增加',
								handler : function() {
									addPower(totlePower_treePanel,userPower_treePanel)
								}
							}, {
								text : '删除',
								handler : function() {
									removePower(userPower_treePanel)
								}
							}]
						}, userPower_treePanel],
						buttons:[{
							text:'保存',
							handler:function(){
								var rootNode=userPower_treePanel.getRootNode();
								var userId=userCombo.getValue();
								var data=new Ext.util.MixedCollection();
								rootNode.eachChild(function(node){
									data.add(node.attributes.id,{
										userId:userId,
										functionCode:node.attributes.id
									})
								})
								
//								log.info('save power...');
//								log.dir(data.items);
								savePower(userId,data.items,function(){});
							}
						}]
					})
			new Ext.Viewport({
						items : [{
							title : '用户权限管理',
							xtype : 'panel',
							width : 800,
							style : 'padding:50px;',
							items : [userInfo_panel,
								new Ext.TabPanel({
									activeItem : 0,
									items : [powerPanel]
								})]
							}]
					})
		});
